import json

from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait

from utils import WebDriverInstance

# 用模拟点击的形式获取所有文章链接，其实可以同时抓取数据。
# 获取前一个地址
def get_prev_url(web_driver, original_url):
    result_list = []
    jump_out = False

    current_url = original_url;
    try:
        while jump_out == False:
            # 打开初始页面
            web_driver.get(current_url)
            print(f"Old URL: {web_driver.current_url}")
            # 等待页面元素加载并找到需要点击的按钮或链接
            wait = WebDriverWait(web_driver, 20)
            print('wait finish...')
            wait.until(EC.element_to_be_clickable((By.ID, 'content_bottom_area')))
            # 获取本页面数据：
            page_item = {
                "title": web_driver.title,
                "url": web_driver.current_url
            }
            result_list.append(page_item)
            print(page_item)
            # 判断 如果 有 上一页的 element 则继续，如果没有，则退出
            prev_eles = web_driver.find_elements(By.CLASS_NAME, 'album_read_nav_prev')
            if len(prev_eles) == 0:
                break
            element = wait.until(EC.element_to_be_clickable((By.CLASS_NAME, 'album_read_nav_prev')))
            # 点击按钮或链接
            element.click()
            print('click finish...')
            # 获取新地址
            current_url = web_driver.current_url
    finally:
        # 关闭浏览器
        web_driver.quit()
        print(f'发生异常，最后一次地址是：{current_url}')
    return result_list


def write_to_file(result_list):
    # 将数据写入 JSON 文件
    with open('demo/exchange_ngn_urls.json', 'w', encoding='utf-8') as f:
        json.dump(result_list, f)
    print("数据已写入 exchange_ngn_urls.json 文件")
    pass


if __name__ == '__main__':
    start_url = 'http://mp.weixin.qq.com/s?__biz=Mzg5NDc0NTIwNA==&mid=2247490595&idx=1&sn=5b1a499717a914fa351815a77ca2d2ce&chksm=c01bb338f76c3a2e73d590021fbdf72b8a5c03e27c478688ab2ff6b9f703516b373883f52bba&scene=178&cur_album_id=3406101716922482691#rd'
    driver = WebDriverInstance().get_driver()
    result = get_prev_url(driver, start_url)
    ## 将结果写入文件
    write_to_file(result)
    print('done')
